class Obj_Reader(object):
    
    def __init__(self, file_name):
        self.file_name = file_name
        self.vertices = []
        self.textures = []
        self.vertex_normals = []
        self.faces = []

        
    def _get_face(self, line):
        polygon = []
        for point in line:
            vertice = []
            point = point.split("/")
            if len(point)==1:
                vertice.append(self.vertices[int(point[0])-1])
                vertice.append([])
                vertice.append([])
            elif len(point)==3:
                if point[1]=="":
                    vertice.append(self.vertices[int(point[0])-1])
                    vertice.append([])
                    vertice.append(self.vertex_normals[int(point[2])-1])
                else:
                    vertice.append(self.vertices[int(point[0])-1])
                    vertice.append(self.textures[int(point[1])-1])
                    vertice.append(self.vertex_normals[int(point[2])-1])
            else:
                raise Exception("Fehlerhafte Datei!")
            polygon.append(vertice)
        print polygon[:3]
        return polygon


        
    def read_file(self):
        f = open(file_name, 'r')

        for line in f.readlines():
            line = line.split()
            if len(line) == 0:
                print "Naechstes"
            elif len(line) != 4:
                raise Exception("Fehlerhafte Datei!")
            elif line[0] == "v":
                self.vertices.append([float(line[1]), float(line[2]),
                                      float(line[3])])
            elif line[0] == "vt":
                self.textures.append([float(line[1]), float(line[2]),
                                      float(line[3])])
            elif line[0] == "vn":
                self.vertex_normals.append([float(line[1]), float(line[2]),
                                            float(line[3])])
            elif line[0] == "f":
                self.faces.append(self._get_face(line[1:]))
            else:
                raise Exception("Fehlerhafte Datei!")

    def __repr__(self):
        output = "hello"
        if len(self.faces)==0:
            output = "Datei wurde noch nicht gelesen mit read_file() oder ist fehlerhaft!"
        else:
            output = "Anzahl der Vertices(Eckpunkte): %s\n" % len(self.vertices)
            output += "Anzahl der Texturen: %s\n" % len(self.textures)
            output += "Anzahl der Vertex-Normalen: %s\n" % len(self.vertex_normals)
            output += "Anzahl der Faces(Polygone): %s" % len(self.faces)
        return output

                
            
if __name__ == "__main__":
    print 'hello hello'
    file_name = "bunny.obj"
    obj_reader = Obj_Reader(file_name)
    obj_reader.read_file()

    

                                
